//销售方案
(function(){
	var db = require("../db/index");
	var config = require("../_include/config");
	var db_table_name = config.dbname.market;
	var db_id_prefix = config.dbIdPrefix.market;
	var helper = require("../_include/helper");

	//构造函数
	var _con = function (market_name,type,args){
		var self = this;
		//ID编号
		self._id = db_id_prefix+Math.random().toString(16).substr(2)+(+new Date).toString(16);
		//销售方案的名称
		self.market_name = market_name;
		//类别标识
		self.type = type;
		try{
			var CoreCon = require("../market."+type+"/_con");
			CoreCon(self,args);
		}catch(e){
			if (!CoreCon) {
				throwCusError("找不到指定销售方案")
			}
			throw e;
		}
		
		db.insert(db_table_name,self);
		console.log("创建一个销售方案：",market_name);
	};
	//PS:模块的暴露放在require skill前面，因为二者互相依赖
	module.exports = _con;
	//技能
	_con.prototype = require("./skill");

	//静态方法
	_con.get = function(Id){
		var result = db.find_by_id(db_table_name,Id);
		if(result){
			result.__proto__ = _con.prototype;
			var type = result.type;
			try{
				var CoreCon = require("../market."+type+"/_con");
				CoreCon(result,args);
			}catch(e){
				if (!CoreCon) {
					throwCusError("找不到指定销售方案")
				}
				throw e;
			}
		}
		return result;
	};
}());
